package com.jiyi.activity.datainput.sm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jiyi.bean.datainput.new_input.BaseBuildBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.picselect.PicsAdapter;
import com.jiyi.widget.picshow.ImagePagerActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.RadioGroupUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/29.
 */

public class BuildBaseInfoReadActivity extends ActivityBase {

    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvAdress)
    TextView tvAdress;
    @BindView(R.id.tvPropertyName)
    TextView tvPropertyName;
    @BindView(R.id.tvAQZRR)
    TextView tvAQZRR;
    @BindView(R.id.tvAQZRRTel)
    TextView tvAQZRRTel;
    @BindView(R.id.tvLXR)
    TextView tvLXR;
    @BindView(R.id.tvLXRTel)
    TextView tvLXRTel;
    @BindView(R.id.tvYWH)
    TextView tvYWH;
    @BindView(R.id.tvYWHZR)
    TextView tvYWHZR;
    @BindView(R.id.tvYWHZRTel)
    TextView tvYWHZRTel;
    @BindView(R.id.tvYWHLXR)
    TextView tvYWHLXR;
    @BindView(R.id.tvYWHLXRTel)
    TextView tvYWHLXRTel;
    @BindView(R.id.tvLouZhang)
    TextView tvLouZhang;
    @BindView(R.id.tvLouZhangTel)
    TextView tvLouZhangTel;
    @BindView(R.id.isPass)
    RadioButton isPass;
    @BindView(R.id.isNoPass)
    RadioButton isNoPass;
    @BindView(R.id.rgIsPass)
    RadioGroup rgIsPass;
    @BindView(R.id.tvWJWH)
    TextView tvWJWH;

    String build_id;

    @BindView(R.id.gvXiu)
    GridView gvXiu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_build_base_info_read);
        ButterKnife.bind(this);
        init();
    }

    private void init()
    {
        getIntentData();
        setTop();
        setViewStatus();

        if (!TextUtils.isEmpty(build_id))
        {
            //修改时，获取数据
            getData();
        }
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("建筑基础属性");
        tvSearch.setVisibility(View.GONE);
    }

    private void setViewStatus() {
        RadioGroupUtil.disableRadioGroup(rgIsPass);
    }

    private void getData() {
        HttpMethods.getInstance().getBaseBuildInfo(UserInfoUtil.getCurrentUserAccessToken(),build_id,2)
                .subscribe(new SampleProgressObserver<BaseBuildBean>(this) {
                    @Override
                    public void onNext(BaseBuildBean o) {

                        fillData(o);
                    }
                });
    }

    private void fillData(BaseBuildBean baseBuildBean)
    {
        tvName.setText(baseBuildBean.getName());
        tvAdress.setText(baseBuildBean.getAddress());
        tvPropertyName.setText(baseBuildBean.getProperty_department());
        tvAQZRR.setText(baseBuildBean.getProperty_response_man());
        tvAQZRRTel.setText(baseBuildBean.getProperty_response_tel());
        tvLXR.setText(baseBuildBean.getProperty_contact_man());
        tvLXRTel.setText(baseBuildBean.getProperty_contact_tel());
        tvYWH.setText(baseBuildBean.getOwners_committee());
        tvYWHZR.setText(baseBuildBean.getCommittee_response_man());
        tvYWHZRTel.setText(baseBuildBean.getCommittee_response_tel());
        tvYWHLXR.setText(baseBuildBean.getCommittee_contact_man());
        tvYWHLXRTel.setText(baseBuildBean.getCommittee_contact_tel());
        tvLouZhang.setText(baseBuildBean.getPeer_advisor());
        tvLouZhangTel.setText(baseBuildBean.getPeer_advisor_tel());
        tvLouZhangTel.setText(baseBuildBean.getPeer_advisor_tel());
        if (baseBuildBean.getIs_accept() == 1)
        {
            isPass.setChecked(true);
        }
        else
            isNoPass.setChecked(true);
        tvWJWH.setText(baseBuildBean.getRecord_number());

        if (baseBuildBean.getImage() == null || baseBuildBean.getImage().size() <= 0) {
            gvXiu.setVisibility(View.GONE);
        } else {
            gvXiu.setVisibility(View.VISIBLE);
            //图片列表
            List<String> picList = (List<String>) baseBuildBean.getImage();
            if (picList != null) {
                final String [] urls = new String [picList.size()];
                for (int i = 0; i < picList.size(); i++) {
                    urls[i] = picList.get(i);
                }

                gvXiu.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Intent intent = new Intent(view.getContext(),ImagePagerActivity.class);
                        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
                        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                        view.getContext().startActivity(intent);
                    }
                });
                PicsAdapter picsAdapter = new PicsAdapter(gvXiu.getContext(),picList);
                gvXiu.setAdapter(picsAdapter);
            }
        }
    }

    @OnClick({R.id.tvBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            default:
                break;
        }
    }
}
